//
//  FieldAction.m
//  MClient
//
//  Created by  史正烨 on 09-11-10.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "FieldAction.h"
#import "MClientAppDelegate.h"
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@implementation FieldAction
@synthesize actionUrls;
@synthesize viewController;

#pragma mark ---- 类基本方法 ----


- (void)dealloc {
	[super dealloc];
    
    [actionUrls release];//huotianjun 增加
    [viewController release];//huotianjun 增加
}

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == [actionUrls count]) {
		// 按下取消按钮
		return;
	}
	NSString *actionUrl = [self.actionUrls objectAtIndex:buttonIndex];
	if ([actionUrl hasPrefix:@"mailto:"]) {
		if([MFMailComposeViewController canSendMail]){
			MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
			NSArray *toRecipients = [[NSArray alloc] initWithObjects:[actionUrl substringFromIndex:7],nil];
			[controller setToRecipients:toRecipients];
			[toRecipients release];
			controller.mailComposeDelegate = self;

			[[viewController.navigationController.viewControllers objectAtIndex:[viewController.navigationController.viewControllers count]-1] presentModalViewController:controller animated:YES];
			[controller release];
		} else {
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错了" message:@"您还没有设置邮件账户" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
	} else if ([actionUrl hasPrefix:@"sms://"]) {
		MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
		picker.messageComposeDelegate= self;
		//picker.body = message; // 默认信息内容
		// 默认收件人(可多个)
		picker.recipients = [NSArray arrayWithObjects:[actionUrl substringFromIndex:6], nil];
		[[viewController.navigationController.viewControllers objectAtIndex:[viewController.navigationController.viewControllers count]-1] presentModalViewController:picker animated:YES];
		[picker release];
	}
	else {
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:actionUrl]];
	}
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
	UIViewController *parentController = [viewController.navigationController.viewControllers objectAtIndex:[viewController.navigationController.viewControllers count]-1];
	
	[parentController becomeFirstResponder];
	[parentController dismissModalViewControllerAnimated:YES];
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller 
				 didFinishWithResult:(MessageComposeResult)result {
//	NSString *msg;
//	switch (result) {
//		case MessageComposeResultCancelled:
//			msg = @"发送取消";
//			break;
//		case MessageComposeResultSent:
//			msg = @"发送成功";
//			[self alertWithTitle:nil msg:msg];
//			break;
//		case MessageComposeResultFailed:
//			msg = @"发送失败";
//			[self alertWithTitle:nil msg:msg];
//			break;
//		default:
//			break;
//	}
//	NSLog(@"发送结果：%@", msg);
	
	UIViewController *parentController = [viewController.navigationController.viewControllers objectAtIndex:[viewController.navigationController.viewControllers count]-1];
	
	[parentController becomeFirstResponder];
	[parentController dismissModalViewControllerAnimated:YES];
}


@end
